package com.zl.learning.io;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

public class FileSimple {
    @Test
    public void test1() {
        File file = new File("./");
        String[] names = file.list(((dir, name) -> !new File(name).isDirectory()));
        assert names != null;
        Arrays.stream(names).forEach(System.out::println);
    }

    @Test
    public void test2() throws IOException {
        FileInputStream fis = new FileInputStream("conf/in.json");
        byte[] buf = new byte[1024];
        int hasRead = 0;
        while ((hasRead = fis.read(buf)) > 0) {
            System.out.println(new String(buf, 0, hasRead));
        }
        fis.close();
    }

    @Test
    public void test3() throws IOException {
        FileWriter fileWriter = new FileWriter("conf/new.txt", true);
//        fileWriter.write("锦瑟 - 李商隐 \n");
//        fileWriter.write("锦瑟无端五十弦");
        fileWriter.append("\n 锦瑟 - 李商隐 \n");
        fileWriter.append("锦瑟无端五十弦");
        fileWriter.close();
    }

    @Test
    public void test4() throws IOException {
        Process p = Runtime.getRuntime().exec("javac");
        try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream(), "GBK"))) {
            String buff;
            while ((buff = br.readLine()) != null) {
                System.out.println(buff);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
